package com.ftg.learn.chapter02.java.reflech;

import java.lang.reflect.Field;

/**
 * 反射 -
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/12/22 下午4:10
 */
public class FirstReflech {

    public static void main(String[] args) {
        Cat c = new Cat();
        c.name = "小花";

        String classPath = "com.ftg.learn.chapter02.java.reflech.Cat";
        //反射 ...
        try {
            //关联对象
            Class<?> cats = Class.forName(classPath);

            //字段
            Field field = cats.getDeclaredField("name");

            //实例  是猫  是猫它爹
            Object obj = cats.newInstance();

            field.set(obj,"小峰|小庆");

            System.out.println(field.get(obj));

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }


    }

}
